#ifndef _ICAR_MSG_H
#define _ICAR_MSG_H

#if defined(__cplusplus)
extern "C" {
#endif

#include "icar.h"
#include "icar_pkg.h"
#include <stdint.h>


#define READ_BUF_LEN		4096
#define READ_MAX_LEN		8192
#define	DATA_MAX_LEN		1024
#define IP_ADDR_LEN			17

struct supplement_req {
	uint16_t sequence;
	uint8_t count;
	uint8_t *data;
};

struct endRegister {
	uint16_t province;
	uint16_t city;
	uint8_t product[5];
	uint16_t model[20];
	uint8_t end_id[7];
	uint8_t plate_color;
	char *plate_mark;
};

struct regiser_res {
	uint16_t sequence;
	uint8_t result;
	char *authcode;
	int len;
};

typedef struct _icarPkg {
	int total;
	int	count;
	uint16_t id;
	uint16_t sequence;
	char number[13];
	uint8_t data[MAX_PKG_LENGTH];
	int len;
	struct _icarPkg *next;
} icarPkg;

typedef struct {
	int clientSock,
		readBufRemain;
	uint8_t readBuf[READ_BUF_LEN],
		 *readBufPtr;
	char  clientAddr[IP_ADDR_LEN];
	icarPkg *package[80];
} request;

int icar_create_server(int type, int port, const char *address);

request * icar_get_connection(int fd, struct timeval *);

int _icar_net_read(int sock, uint8_t *buf, int len);

int _icar_readChar(request *r, uint8_t *ch);

int _icar_readPkg(request *r, uint8_t *destBuf, int len);

int icar_read_request(int fd, request *r);

int icar_process_request(int fd, request *r);

void icar_end_request(request *r);


#if defined(__cplusplus)
}
#endif

#endif
